package com.dht.xiao.jianye.command.base.util;

import android.text.TextPaint;
import android.widget.TextView;

import java.util.List;

/**
 * @author :      fangbingran
 * @aescription :工具
 * @date :        2019/06/15  11:38
 */
public class Utils {
    public static <T> T checkNotNull(T t, String message) {
        if (t == null) {
            throw new NullPointerException(message);
        }
        return t;
    }

    public static int parseInt(String value) {
        int parseResult = 0;
        try {
            parseResult = Integer.parseInt(value);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            parseResult = 0;
        }
        return parseResult;
    }

    private static char[] mTextCharArray;
    private static int length;
    private static int index;

    public static String stringLength(List<String> datas) {
        if (datas == null || datas.size() == 0) {
            return "";
        }
        length = 0;
        index = 0;
        for (String data :
                datas) {
            mTextCharArray = data.toCharArray();
            while (index < mTextCharArray.length) {
                char c = mTextCharArray[index++];
                if (c < 128) {
                    length = length + 1;
                } else {
                    length = length + 2;
                }
            }
        }
        StringBuffer buffer = new StringBuffer();
        int index = length % 3;
        length = length - index + 3;
        for (int i = 0; i < length / 3; i++) {
            buffer.append("糕");
        }
        return buffer.toString();
    }
    /**
     * 计算出该TextView中文字的长度(像素)
     */
    public static float getTextViewLength(TextView textView, String text) {
        TextPaint paint = textView.getPaint();
// 得到使用该paint写上text的时候,像素为多少
        float textLength = paint.measureText(text);
        return textLength;
    }
}
